﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ExcelHelper
{
    public static class StringParser
    {
        public static object Parse(Type type, string str)
        {
            try
            {
                if (type == typeof(string))
                {
                    return str;
                }
                else if (type == typeof(int))
                {
                    return int.Parse(str);
                }
                else if (type == typeof(UInt32))
                {
                    return UInt32.Parse(str);
                }
                else if (type == typeof(long))
                {
                    return long.Parse(str);
                }
                else if (type == typeof(UInt64))
                {
                    return UInt64.Parse(str);
                }
                else if (type == typeof(double))
                {
                    return double.Parse(str);
                }
                else if (type == typeof(double?))
                {
                    if (string.IsNullOrWhiteSpace(str))
                    {
                        return null;
                    }
                    else
                    {
                        return double.Parse(str);
                    }
                }
                else if (type == typeof(DateTime))
                {
                    return _GetDateTimeFromExcelString(str);
                    
                }
                else if (type == typeof(DateTime?))
                {
                    if (string.IsNullOrWhiteSpace(str))
                    {
                        return null;
                    }
                    else
                    {
                        return _GetDateTimeFromExcelString(str);
                    }
                    
                }
                else if (type == typeof(DateTimeOffset))
                {
                    return DateTimeOffset.Parse(str);
                }
                else if (type == typeof(float))
                {
                    return float.Parse(str);
                }
                else if (type == typeof(bool))
                {
                    return bool.Parse(str);
                }
                else if (type.IsEnum)
                {
                    return Enum.Parse(type, str);
                }

                return null;
            }
            catch
            {
                return null;
            }
        }

        static DateTime _GetDateTimeFromExcelString(string str)
        {
            try
            {
                return DateTime.Parse(str);//正常的日期格式

            }
            catch
            {
                try//excel中保存为数字的格式
                {
                    double val = double.Parse(str)-2;
                    //与"1900/1/1"的相差的天数

                    //天
                    double days = Math.Floor(val);


                    //小时
                    val -= days;
                    val *= 24;
                    double hrs = Math.Floor(val);

                    //分
                    val -= hrs;
                    val *= 60;
                    double mins = Math.Floor(val);

                    //秒
                    val -= mins;
                    val *= 60;
                    double seconds = Math.Floor(val);

                    //
                    var date = new DateTime(1900, 1, 1);
                    var re=date+ new TimeSpan((int)days, (int)hrs, (int)mins, (int)seconds);

                    return re;



                }

                catch
                {
                    return new DateTime(1900, 1, 1);
                }
            }
        }
    }
}
